package com.example.photoview;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import java.awt.*;
import java.util.ArrayList;
import java.util.Optional;

public class RenameAction {
    private  GridPane grid=new GridPane();
    private Stage anotherStage=new Stage();
    private  Label msg=new Label();
    private Button submit;
    final private TextField name=new TextField();
    final private TextField starNum=new TextField();
    final private  TextField bitNum=new TextField();
    private HomeController hc = (HomeController) ControllerUtil.controllers.get(HomeController.class.getSimpleName());
    ArrayList<ImageModel> sourceList = SelectionModel.getImageModels();//存放图片本身的List

    public RenameAction() {

    }

    /**
     * 开始进行重命名功能
     * @param imageBox
     */
   public RenameAction(ImageBox imageBox){

       //ControllerUtil.controllers.put(this.getClass().getSimpleName(), this);
       //hc = (HomeController) ControllerUtil.controllers.get(HomeController.class.getSimpleName());

        grid.setPadding(new Insets(10,10,10,10));
        grid.setVgap(5);
        grid.setHgap(5);
        submit=new Button("完成");
        Label label1 = new Label("名称");
        GridPane.setConstraints(label1, 0, 0);
        name.setPromptText("请输入新名字");
        name.setPrefColumnCount(20);
        name.getText();
        GridPane.setConstraints(name, 1, 0);
        grid.getChildren().addAll(label1,name);
        if(sourceList.isEmpty()|| sourceList.size()==1){//单选
            SelectedModel.setSourcePath((imageBox.getImageModel()).getImageFilePath());
            TextInputDialog dialog1=new TextInputDialog(imageBox.getImageModel().getImageName());
            dialog1.setTitle("重命名");
            dialog1.setHeaderText(null);
            dialog1.setContentText("请输入名称");
            Optional<String> result=dialog1.showAndWait();
            if(result.isPresent()){
                SelectedModel.renameImage(result.get());
            }
            hc.refreshImagesList(hc.getSortComboBox().getValue());
            imageBox.getContextMenu().hide();
        }else{
            System.out.println("sourceList.size()"+sourceList.size());
            SelectedModel.setSourcePath(sourceList);
//            TextInputDialog dialog1=new TextInputDialog(imageBox.getImageModel().getImageName());
//            dialog1.setTitle("重命名");
//            dialog1.setHeaderText(null);
//            dialog1.setContentText("请输入:");
//            Optional<String> result=dialog1.showAndWait();
//            if(result.isPresent()){
//                SelectedModel.renameImage(result.get());
//            }
            Label label2=new Label("起始编号");
            GridPane.setConstraints(label2,0,1);
            starNum.setPromptText("请输入起始编号");
            starNum.setPrefColumnCount(15);
            starNum.getText();
            GridPane.setConstraints(starNum,1,1);
            Label label3=new Label("编号位数");
            GridPane.setConstraints(label3, 0, 2);
            bitNum.setPromptText("请输入编号位数");
            bitNum.setPrefColumnCount(10);
            bitNum.getText();
            GridPane.setConstraints(bitNum, 1, 2);
            GridPane.setConstraints(msg, 1, 3);
            GridPane.setConstraints(submit,0,3);
            grid.getChildren().addAll( label2, starNum,label3,submit,bitNum,msg);
            submit.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    System.out.println(bitNum.getText());
                    System.out.println(starNum.getText());

                   // if()

                    Integer it = new Integer(starNum.getText());
                    int i =  it.intValue();//Integer.getInteger(starNum.getText())
                    System.out.println(i);
                    hc.setLengthNum(bitNum.getText());
                    hc.setStartNum(i);//java.lang.NullPointerException

                    SelectedModel.renameImage(name.getText());
                    hc.refreshImagesList(hc.getSortComboBox().getValue());
                   anotherStage.hide();
                }
            });
            Scene scene=new Scene(grid);
            anotherStage.setTitle("重命名");
            anotherStage.setScene(scene);
            anotherStage.show();

            imageBox.getContextMenu().hide();
        }

    }
}
